﻿Imports UtilitiesLibrary

Public Class registration
    Inherits System.Web.UI.Page
    Private thisUser As New user
    Private objDB = New DBConnect

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("db") Is Nothing Then
                Session("db") = objDB
            End If
            If Session("user") Is Nothing Then
                Session("user") = thisUser
            End If
            objDB = Session("db")
            thisUser = Session("user")
        End If
    End Sub

    Protected Sub btnRegSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegSubmit.Click
        'on click this sub will collect all data from form fields and copy to thisUse

        Dim isValid As Boolean = True
        'validation
        'make sure not blank 
        If txtFirstName.Text = "" Then
            lblFirstNameValidator.Visible = True
            lblFirstNameValidator.Text = "*"
            lblFirstNameValidator.ForeColor = Drawing.Color.Red
            'lblMsg.Visible = True
        Else
            lblFirstNameValidator.Visible = False
            ' lblMsg.Visible = False
        End If

        If txtLastName.Text = "" Then
            lblLastNameValidator.Visible = True
            lblLastNameValidator.Text = "*"
            lblLastNameValidator.ForeColor = Drawing.Color.Red
            'lblMsg.Visible = True
        Else
            lblLastNameValidator.Visible = False
            'lblMsg.Visible = False
        End If

        If txtPseudonym.Text = "" Then
            lblPseudonymValidator.Visible = True
            lblPseudonymValidator.Text = "*"
            lblPseudonymValidator.ForeColor = Drawing.Color.Red
            'lblMsg.Visible = True
        Else
            lblPseudonymValidator.Visible = False
            ' lblMsg.Visible = False
        End If

        If txtPassword.Text = "" Then
            lblPasswordValidator.Visible = True
            lblPasswordValidator.Text = "*"
            lblPasswordValidator.ForeColor = Drawing.Color.Red
            'lblMsg.Visible = True
        Else
            lblPasswordValidator.Visible = False
            'lblMsg.Visible = False
        End If

        'makre sure no illegal characters
        'make sure unique pseudonym

        Dim sql As String = ""
        'collect data from form

        If isValid Then
            thisUser.firstname = txtFirstName.Text
            thisUser.lastname = txtLastName.Text
            thisUser.pseudonym = txtPseudonym.Text
            thisUser.password = txtPassword.Text

            'MsgBox("Values in thisUser Object:" & vbCr & "Name: " & thisUser.firstname & " " & thisUser.lastname & vbCr & "Pseudonym: " & _
            '       thisUser.pseudonym & vbCr & "Password: " & thisUser.password) 'display values in thisUser

            sql = "INSERT INTO Participant (FirstName, LastName, Pseudonym, Password) Values('" & thisUser.firstname & "','" & thisUser.lastname & "','" & thisUser.pseudonym & _
                "','" & thisUser.password & "')" 'SQL insert String

            ' MsgBox("SQL String: " & vbCr & sql) 'output SQL string to msgbox

            objDB.doUpdate(sql) 'send string to database

        End If
    End Sub

    Protected Sub btnRegClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegClear.Click
        'clears all form fields and data in memory

        thisUser.firstname = ""
        thisUser.lastname = ""
        thisUser.pseudonym = ""
        thisUser.password = ""

        Me.txtFirstName.Text = ""
        Me.txtLastName.Text = ""
        Me.txtPseudonym.Text = ""
        Me.txtPassword.Text = ""
        Session("user") = thisUser
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        'submits username and password for authentication
        Dim password As String = ""
        Dim count As Integer = 0
        Dim IsValid As Boolean = True
        Dim Sql As String
        'validate 

        'authenticate

        Sql = "SELECT * FROM Participant WHERE Psuedonym ='" & txtLoginPseudonym.Text & "'"
        objDB.getDataSet(Sql, count)

        password = objDB.getField("Password", 0)

        If txtLoginPassword.Text = password Then
            ' user is authenticated
            ' load user into session
            thisUser.firstname = objDB.getfield("Firstname", 0)
            thisUser.lastname = objDB.getField("Lastname", 0)
            thisUser.pseudonym = objDB.getField("Pseudonym", 0)
            thisUser.password = objDB.getField("Password", 0)
            thisUser.participantID = objDB.getField("ParticipantID", 0)
            Session("user") = thisUser
        End If
    End Sub

    Protected Sub btnLoginClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoginClear.Click
        txtLoginPseudonym.Text = ""
        txtLoginPassword.Text = ""
    End Sub
End Class